package com.yoouce.patchmng.controller;

import com.yoouce.patchmng.bean.UserInfo;
import com.yoouce.patchmng.init.SystemContants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

/**
 * 系统登陆类
 * Created by administrator on 15-9-29.
 */
@Controller
public class LoginController extends BaseController {

    @RequestMapping(value = "/login")
    public String login(UserInfo userInfo) {
        //获取系统中已有的用户信息列表
        Map<String, UserInfo> userInfoMap = SystemContants.getUsersMap();
        UserInfo existUser = userInfoMap.get(userInfo.getUserName());

        if (userInfoMap.containsKey(userInfo.getUserName()) && userInfo.getUserPassword().equals(existUser.getUserPassword())) {
            getSession().setAttribute(SystemContants.USER_KEY, existUser);
            return "redirect:/index";
        } else {
            return "login";
        }
    }



    @RequestMapping(value = "/logout")
    public String logout() {
        //获取系统中已有的用户信息列表
        getSession().setAttribute(SystemContants.USER_KEY, "");
        return "login";

    }


    @RequestMapping(value = "/index")
    public String index() {
        //获取Session中用户信息
        UserInfo userInfo = (UserInfo) getSession().getAttribute(SystemContants.USER_KEY);
        getRequest().setAttribute("userInfo",userInfo);
       return "index";

    }
}
